package com.ling.service;

import com.ling.bean.Question;
import com.ling.bean.QuestionExample;
import com.ling.dao.QuestionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class QuestionService {
    private final QuestionMapper questionMapper;

    @Autowired
    public QuestionService(QuestionMapper questionMapper) {
        this.questionMapper = questionMapper;
    }

    //查询用户提过的问题
    public List<Question> getUserQuestion(String userId) {
        QuestionExample questionExample = new QuestionExample();
        //按照发布问题的时间逆序排列
        questionExample.setOrderByClause("editTime DESC");
        questionExample.createCriteria().andUseridEqualTo(userId);
        return questionMapper.selectByExample(questionExample);
    }

    public void saveQuestion(Question question) {
        questionMapper.insertSelective(question);
    }

    //删除问题
    public void deleteQuestionById(String questionId) {
        questionMapper.deleteByPrimaryKey(questionId);
    }

    //修改问题
    public void updateQuestion(Question question) {
        questionMapper.updateByPrimaryKeySelective(question);
    }

    //根据id获取question
    public Question getUserQuestionById(String questionId) {
        return questionMapper.selectByPrimaryKey(questionId);
    }

    //查询指定areaId下的所有问题
    public List<Question> getAllQuestionsByAreaId(String areaId) {
        QuestionExample questionExample = new QuestionExample();
        questionExample.createCriteria().andAreaidEqualTo(areaId);
        return questionMapper.selectByExample(questionExample);
    }

    //查询所有的问题
    public List<Question> getAllQuestions() {
        QuestionExample questionExample = new QuestionExample();
        return questionMapper.selectByExample(questionExample);
    }


}
